/**
 * Created by Administrator on 2017/8/15.
 */
$(document).ready(function () {

    if (C.token==undefined||C.token==null){
        alert('请登录');
        //location.href='login.html';
        againLogin();
        return ;
    }
    //ajax加载信息
    var orderTpl=$('#order-template').html();
    var orderCmp=Handlebars.compile(orderTpl);

    var range=200,//距下边界长度/单位px
        totalheight = 0;
    var orderPageLength=10,//每页数据条数
        orderMaxnum = 0, //设置数据总数
        orderNum = 0, //当前数量
        orderFlag=0,
        orderStart='0';

    getOrderInfo('noFahuo');


    //监听滚动高度，加载数据
    $(window).on('scroll',function () {
        var srollPos = $(window).scrollTop(); //滚动条距顶部距离(页面超出窗口的高度)
        //console.log("滚动条到顶部的垂直高度: " + $(document).scrollTop());
        //console.log("页面的文档高度 ："+$(document).height());
        //console.log('浏览器的高度：'+$(window).height());
        totalheight = parseFloat($(window).height()) + parseFloat(srollPos);

        if (orderNum>=orderMaxnum){
            return;
        }
        if (($(document).height()-range)<=totalheight&&orderNum<orderMaxnum){
            //这里不能使用模板填充，应该是append()....
            getOrderInfo('noFahuo');
        }
    });

    /*查看我的订单状态*/

    /**
     * getOrderInfo
     * @ param
     * noPay      待支付
     * noConfirm  待收货
     * noFeedback 待评价
     * comeback   退换货
     * noFahuo    待发货
     * */
    function getOrderInfo(orderType) {
        if (orderFlag){
            return;
        }
        $('.loading').show();
        orderFlag=1;

        $.ajax({
            url:C.interface.myOrder,
            type:'get',
            dataType:'json',
            data:{
                orderType:orderType,
                token:C.token,
                start:orderStart,
                length:orderPageLength
            },
            success:function (response) {
                if(response.result=='success'){
                    $('.loading').hide();
                    orderNum++;
                    var data=response.data;
                    orderMaxnum=data['totalCount'];
                    orderStart=orderNum*orderPageLength;
                    if(orderMaxnum==0){
                        //alert('没有数据');
                        $('.no-info').show();

                    }
                    if (data.products.length>0) {
                        //$('.no-info').show();
                        //alert('没有数据');
                        $('#none-pay').append(orderCmp(data));
                    }else {
                        $('.no-info').show();
                        orderNum=orderMaxnum+1;
                    }
                    orderFlag=0;
                    new auiLazyload({
                        errorImage:'../image/error-img.png'
                    });
                }else if(response.result == 'login' ){
                    //location.href='login.html';
                    alert('登录过期请重新登录');
                    againLogin();
                }else{
                    alert(response.errorMsg);
                }
            }
        })
    }


});

/*
 $(document).ready(function () {
 new auiLazyload({
 errorImage:'../image/error-img.png'
 });
 });*/
